package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Spinner;

import com.example.testapplication.adapter.PlanetGridAdapter;
import com.example.testapplication.bean.Planet;
import com.example.testapplication.util.Utils;

import java.util.ArrayList;

public class GridViewActivity extends AppCompatActivity {
    private final static String TAG = "GridViewActivity";
    private GridView gv_planet; // 声明一个网格视图对象
    private ArrayList<Planet> planetList = new ArrayList<>(); // 声明一个行星队列
    private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
            R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
    private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
    private static String[] descArray = {
            "水星是太阳系八大行星最内侧也是最小的一颗行星，也是离太阳最近的行星",
            "金星是太阳系八大行星之一，排行第二，距离太阳0.725天文单位",
            "地球是太阳系八大行星之一，排行第三，也是太阳系中直径、质量和密度最大的类地行星，距离太阳1.5亿公里",
            "火星是太阳系八大行星之一，排行第四，属于类地行星，直径约为地球的53%",
            "木星是太阳系八大行星中体积最大、自转最快的行星，排行第五。它的质量为太阳的千分之一，但为太阳系中其它七大行星质量总和的2.5倍",
            "土星为太阳系八大行星之一，排行第六，体积仅次于木星"
    };
    private String[] dividerArray = {
            "不显示分隔线",
            "只显示内部分隔线(NO_STRETCH)",
            "只显示内部分隔线(COLUMN_WIDTH)",
            "只显示内部分隔线(STRETCH_SPACING)",
            "只显示内部分隔线(SPACING_UNIFORM)",
            "显示全部分隔线(看我用padding大法)"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);
        for (int i = 0; i < iconArray.length; i++) {
            planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
        }

        // 构建一个行星队列的网格适配器
        PlanetGridAdapter adapter = new PlanetGridAdapter(this,planetList);
        // 从布局视图中获取名叫gv_planet的网格视图
        gv_planet = findViewById(R.id.gv_plenet);
        // 给gv_planet设置行星网格适配器
        gv_planet.setAdapter(adapter);
        // 设置点击监听器
        gv_planet.setOnItemClickListener(adapter);
        gv_planet.setOnItemClickListener(adapter);
        // 初始化分割线下拉框
        initDividerSpinner();
    }

    private void initDividerSpinner() {
        ArrayAdapter<String> dividerAdapter = new ArrayAdapter<String>(this,R.layout.item_select,dividerArray);
        Spinner sp_grid = findViewById(R.id.sp_grid);
        sp_grid.setPrompt("请选择分割线显示方式");
        sp_grid.setAdapter(dividerAdapter);
        sp_grid.setOnItemSelectedListener(new DividerSelectedListener());
        sp_grid.setSelection(0);
    }

    private class DividerSelectedListener implements android.widget.AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            int dividerPad = Utils.dip2px(GridViewActivity.this,2);// 定义间隔为2do
            gv_planet.setBackgroundColor(Color.RED); // 设置gv_planet的背景颜色
            gv_planet.setHorizontalSpacing(dividerPad);
            gv_planet.setVerticalSpacing(dividerPad);
            gv_planet.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            gv_planet.setColumnWidth(Utils.dip2px(GridViewActivity.this,120));
            gv_planet.setPadding(0,0,0,0);
            if (i == 0) {  // 不显示分隔线
                gv_planet.setBackgroundColor(Color.WHITE);
                gv_planet.setHorizontalSpacing(0);
                gv_planet.setVerticalSpacing(0);
            } else if (i == 1) {  // 只显示内部分隔线(NO_STRETCH)
                gv_planet.setStretchMode(GridView.NO_STRETCH);
            } else if (i == 2) {  // 只显示内部分隔线(COLUMN_WIDTH)
                gv_planet.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
            } else if (i == 3) {  // 只显示内部分隔线(STRETCH_SPACING)
                gv_planet.setStretchMode(GridView.STRETCH_SPACING);
            } else if (i == 4) {  // 只显示内部分隔线(SPACING_UNIFORM)
                gv_planet.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
            } else if (i == 5) {  // 显示全部分隔线（使用padding）
                gv_planet.setPadding(dividerPad, dividerPad, dividerPad, dividerPad);
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }
}